<?php

// Get all variables
$database = $_POST['database'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$user_table = $_POST['user_table'];
//$users_pages = $_POST['users_pages'];
$role_table = $_POST['role_table'];
$directory = $_POST['directory'];
$site_name = $_POST['site_name'];
$page_afterreg = $_POST['page_afterreg'];
$pi_subdir = $_POST['pi_subdir'];
$mailer_dir = $_POST['mailer_dir'];
$defaultpage = $_POST['defaultpage'];
$msg_badlogin = $_POST['msg_badlogin'];
$connect_file = $_POST['connect_file'];
$query_file = $_POST['query_file'];
$ssl_port = $_POST['ssl_port'];
$use_ssl = $_POST['use_ssl'];
$msg_username = $_POST['msg_username'];
$msg_password = $_POST['msg_password'];
$cf_subdir = $_POST['cf_subdir'];

$do_write_connect = $_POST['write_connect'];

// Get file text from template
$config_file = file_get_contents('regsettings_tpl.php');

// Make replacements
$config_file = str_replace('%database%',$database,$config_file);
$config_file = str_replace('%user%',$user,$config_file);
$config_file = str_replace('%pass%',$pass,$config_file);
$config_file = str_replace('%user_table%',$user_table,$config_file);
//$config_file = str_replace('%users_pages%',$users_pages,$config_file);
$config_file = str_replace('%role_table%',$role_table,$config_file);
$config_file = str_replace('%directory%',$directory,$config_file);
$config_file = str_replace('%site_name%',$site_name,$config_file);
$config_file = str_replace('%page_afterreg%',$page_afterreg,$config_file);
$config_file = str_replace('%pi_subdir%',$pi_subdir,$config_file);
$config_file = str_replace('%mailer_dir%',$mailer_dir,$config_file);
$config_file = str_replace('%defaultpage%',$defaultpage,$config_file);
$config_file = str_replace('%msg_badlogin%',$msg_badlogin,$config_file);
$config_file = str_replace('%cf_subdir%',$cf_subdir,$config_file);
$config_file = str_replace('%connect_file%',$connect_file,$config_file);
$config_file = str_replace('%query_file%',$query_file,$config_file);
$config_file = str_replace('%ssl_port%',$ssl_port,$config_file);
$config_file = str_replace('%use_ssl%',$use_ssl,$config_file);
$config_file = str_replace('%msg_username%',$msg_username,$config_file);
$config_file = str_replace('%msg_password%',$msg_password,$config_file);

// Make connection file
$connect_dump = "<?php
\$conn = mysql_connect('localhost', '$user', '$pass') or die(mysql_error());
mysql_select_db('$database',\$conn)  or die(mysql_error());
?>";



// write out new file

if($_REQUEST['output'] == 'screen')	{
    	echo "<html>\n<head>\n</head>\n<body>";
		echo "<p>Edit the 'regsetting.php' file and put the following inside it (after php tags)</p>\n";		
        echo "<TEXTAREA NAME=\"config_dump\" COLS='80' ROWS='20'>$config_file</TEXTAREA>\n";
		echo "Follow this link to <a href='setup.php'>setup additional tables</a> for permission system.";
		echo "<br /> Write the following into the file $connect_file (do not do this if an existing connection file was specified) ";
		echo "<TEXTAREA NAME=\"connect_dump\" COLS='80' ROWS='20'>$connect_dump</TEXTAREA>\n";

		echo "</body></html>";


    }else{
        file_put_contents('../regsettings.php',$config_file);
		$msg = "If there were no errors, the settings file was written to regsettings.php";
		echo "<html>\n<head>\n</head>\n<body>";
		echo "<h3>$msg</h3>";
		echo "Follow this link to <a href='setup.php'>setup additional tables</a> for permission system.";
        echo "</body></html>";
		}

	if($do_write_connect == 1){
		echo 'Writing connect file';
		file_put_contents("../" . $connect_file,$connect_dump);
	}
		
?>